// pages/addPicker/index.js
const api = require('../../config/api.js');
const apiDic = require('../../config/api.js');
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    private:{
      name:"<<隐私协议>>",
      url:"https://www.souyasou.cn/profile/upload/2021/12/21/00b211fa-42f2-468c-a8d9-bd55ba81d518.pdf"
    },
    width:'',
    show:false,
    invoiceTypes:[],
    invoice:{}
  },
  openPrivate(e){
    let that = this
    wx.downloadFile({
      url: that.data.private.url,
      success: function (res) {
        console.log(res)
        var filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功', res)
          }
        })
      }
    })
  },
  showPopup(e) {
		this.setData({
			show: true
		})
  },
  onClose() {value
		this.setData({
			show: false
		})
  },
  onConfirm(e) {
    let invoiceTypeName  = e.detail.value.dictLabel
    let invoiceType = e.detail.value.dictValue
		this.setData({
      'invoice.invoiceType': invoiceType,
      'invoice.invoiceTypeName': invoiceTypeName,
			show: false
		})
	},
  /**
   * 提交发票抬头信息
   */
  addPicker(e){
    let this_=this;
    if(!this_.data.invoice.companyName){
      return Notify({ type: 'warning', message: '请输入单位名称' })
    }
    if(!this_.data.invoice.companyNumber){
      return Notify({ type: 'warning', message: '请输入税号' })
    }
    if(!this_.data.invoice.invoiceType){
      return Notify({ type: 'warning', message: '请选择发票类型' })
    }
    wx.request({
      url: api.updateWxUserInfo,
      method:'PUT',
      data:{
        invoiceTitle:JSON.stringify(this_.data.invoice),
        id:this_.data.userId
      },
      success:(res)=>{
        wx.hideLoading();
        if(res.data.code == 200){
          if(res.data.code == 200){
            this_.setData({
              vipList : res.data.data
            })
          }else{
            wx.showToast({
              title: res.data.msg
            })
          }
         wx.switchTab({
           url: '/pages/my/index',
         })
        }
      },
    })
  },
  //invoice_type

  getInvoiceTypes(){
    wx.request({
      url: apiDic.dictDataListUrl,
      data: {
        type: 'invoice_type'
      },
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          let data = res.data.data
          this.setData({
            invoiceTypes: data
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.invoiceTitle!=null){
      this.setData({
        invoice:JSON.parse(options.invoiceTitle)
     })
    }
    this.getInvoiceTypes()
    let userId = options.userId;
    this.setData({
       userId:userId
    })
    let size = wx.getStorageSync('windowWidth')
    this.setData({
      width:size
    })
    
  },
  onChange(event){
    if(event.currentTarget.dataset.name =='companyName'){
      this.setData({
        'invoice.companyName':event.detail
      })
    }else if(event.currentTarget.dataset.name =='companyNumber'){
      this.setData({
        'invoice.companyNumber':event.detail
      })
    }else if(event.currentTarget.dataset.name =='address'){
      this.setData({
        'invoice.address':event.detail
      })
    }else if(event.currentTarget.dataset.name =='phone'){
      this.setData({
        'invoice.phone':event.detail
      })
    }else if(event.currentTarget.dataset.name =='email'){
      this.setData({
        'invoice.email':event.detail
      })
    }else if(event.currentTarget.dataset.name =='invoiceType'){
      this.setData({
        'invoice.invoiceType':event.detail
      })
    }else if(event.currentTarget.dataset.name =='bankName'){
      this.setData({
        'invoice.bankName':event.detail
      })
    }else if(event.currentTarget.dataset.name =='bankNumber'){
      this.setData({
        'invoice.bankNumber':event.detail
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})